package org.example.market.controller;


import org.example.market.config.UserQuery;

import org.example.market.entity.User;
import org.example.market.service.UserService.UserService;
import org.example.market.util.PageResult;
import org.example.market.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result<User> addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @GetMapping
    public Result<List<User>> getUserList() {
        return userService.getAllUserList();
    }

    @GetMapping("/page")
    public Result<PageResult<User>> getUserList( UserQuery query) {
        return userService.findByPage(
                query.getCurrentPage(),
                query.getPageSize(),
                query.getUserCode(),
                query.getUserName()
        );
    }

    @GetMapping("/{id}")
    public Result<User> getUserDetail(@PathVariable Long id) {
        return userService.getUserDetail(id);
    }

    @PutMapping("/update")
    public Result<User> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        return userService.deleteUser(id);
    }

    @PostMapping("/login")
    public Result<Map<String, Object>> login(
            @RequestParam String userCode,
            @RequestParam String userPassword) {
        return userService.login(userCode, userPassword);
    }

    @PostMapping("/logout")
    public Result<Void> logout() {
        return Result.success();
    }

    @PostMapping("/changePassword")
    public Result<Void> changePassword(
            @RequestParam Long id,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        return userService.changePassword(id, oldPassword, newPassword);
    }

    @PostMapping("/updateStatus")
    public Result<Void> updateStatus(
            @RequestParam Long id,
            @RequestParam Integer status) {
        return userService.updateStatus(id, status);
    }

    @PostMapping("/updateRole")
    public Result<Void> updateRole(
            @RequestParam Long id,
            @RequestParam Integer status) {
        return userService.updateRole(id, status);
    }

    @PutMapping("/profile")
    public Result<User> updateProfile(@RequestBody User user) {
        return userService.updateUser(user);
    }
}